
package com.insigma.business.qrcode.soldier.service.impl;



import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.soldier.dao.PlacementPositionInfoDao;
import com.insigma.business.qrcode.soldier.dto.PlacementPositionInfoBSearcher;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.entity.PlacementPositionInfoDO;
import com.insigma.business.qrcode.soldier.service.PlacementPositionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("placementPositionInfoService")
public class PlacementPositionInfoServiceImpl implements PlacementPositionInfoService {

    @Autowired(required = false)
    private PlacementPositionInfoDao placementPositionInfoDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    
    @Override
    public List<PlacementPositionInfoDO> search(final PlacementPositionInfoBSearcher placementPositionInfoBSearcher) {




        return placementPositionInfoDao.search(placementPositionInfoBSearcher);
    }

    
    @Override
    public PlacementPositionInfoDO get(final String id) {
        return placementPositionInfoDao.get(id);
    }

    
    @Override
    public Integer insert(final PlacementPositionInfoDO placementPositionInfoDO) {
        placementPositionInfoDO.setId(sequenceGenerator.genKey(SequenceNameConstant.PLACEMENT_POSITION_INFO_ID));
        return placementPositionInfoDao.insert(placementPositionInfoDO);
    }

    
    @Override
    public Integer update(final PlacementPositionInfoDO placementPositionInfoDO) {
        return placementPositionInfoDao.update(placementPositionInfoDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId) {
        return placementPositionInfoDao.delete(id, modifierType, modifierId);
    }

    @Override
    public List<PlacementPositionInfoDO> listByBatchId(String batchId) {
        return placementPositionInfoDao.listByBatchId(batchId);
    }

    @Override
    public Integer publishAllPosition(String batchId, String modifierId, String modifierType) {
        return placementPositionInfoDao.publishAllPosition(batchId, modifierId, modifierType);
    }

    @Override
    public PlacementPositionInfoDO getByUnitNameAndNameAndType(String unitName, String name, String type) {
        return placementPositionInfoDao.getByUnitNameAndNameAndType(unitName, name, type);
    }

    @Override
    public Integer transferQuota(String from, String to, Integer quota) {
        PlacementPositionInfoDO fromPosition = placementPositionInfoDao.get(from);
        PlacementPositionInfoDO toPosition = placementPositionInfoDao.get(to);
        while (true) {
            BizCheckUtil.checkIsTrue(fromPosition.getQuota() >= quota, "配额不足");
            Integer integer = placementPositionInfoDao.addQuota(from, fromPosition.getQuota(), 0 - quota);
            if (integer > 0) {
                break;
            }
            fromPosition = placementPositionInfoDao.get(from);
        }

        while (true) {
            Integer integer = placementPositionInfoDao.addQuota(to, toPosition.getQuota(), quota);
            if (integer > 0) {
                break;
            }
            toPosition = placementPositionInfoDao.get(to);
        }

        return 1;
    }

    @Override
    public List<PlacementPositionInfoDO> listByIdCardNoAndBatchType(String idCardNo, String type) {
        return placementPositionInfoDao.listByIdCardNoAndBatchType(idCardNo, type);
    }

    @Override
    public PlacementPositionInfoDO getByBatchIdAndUnitAndName(String currentBatchId, String unitId, String name) {
        return placementPositionInfoDao.getByBatchIdAndUnitAndName(currentBatchId, unitId, name);
    }

    @Override
    public PlacementPositionInfoDO getByCode(String code) {
        return placementPositionInfoDao.getByCode(code);
    }


}
